//
//  DateTimePicker.swift
//  ZYA
//
//  Created by 邱仙凯 on 2021/11/23.
//  Copyright © 2021 aowang. All rights reserved.
//

import UIKit
 
class DateTimePicker:UIViewController {
  var contentV:UIView!
   var bg:UIView!
    var selectTime = Date.nowTimeToSecond
    
  var resultClosure:((String)->())?
    override func viewDidLoad() {
        super.viewDidLoad()
     
       setup()
  }
  
   lazy var dateTPicker:UIDatePicker = {
       //创建日期选择器
       let datePicker = UIDatePicker()
       
         //将日期选择器区域设置为中文，则选择器日期显示为中文
         datePicker.locale = Locale(identifier: "zh_GB")
       if #available(iOS 13.4, *) {
           datePicker.preferredDatePickerStyle = .wheels
       } else {
           
       }
         //注意：action里面的方法名后面需要加个冒号“：”
         datePicker.addTarget(self, action: #selector(dateChanged),
                              for: .valueChanged)
         return datePicker
   }()
 
   lazy var confirmBtn:UIButton = {
      let v = UIButton()
       v.layer.cornerRadius = 16
       v.layer.borderWidth = 1
       v.setTitle("确认", for: .normal)
       v.setTitleColor(Theme.primaryColor(), for: .normal)
       v.layer.borderColor = Theme.primaryColor().cgColor
       return v
   }()
  func setup() {
      self.view.isUserInteractionEnabled = true
      let gr = UITapGestureRecognizer(target: self, action: #selector(close))
      gr.numberOfTapsRequired = 1
      bg = UIView(frame: CGRect(x:0,y: 0 ,width: KSGlobal.screenW,height: KSGlobal.screenH))
      bg.isUserInteractionEnabled = true
      bg.backgroundColor = UIColor.black.withAlphaComponent(0.6)
      view.addSubview(bg)
      bg.addGestureRecognizer(gr)
      
      
   
       contentV = UIView(frame: CGRect(x:20,y:(KSGlobal.screenH - 260)/2,width: KSGlobal.screenW - 40,height: 300))
       contentV.layer.cornerRadius = 5
       contentV.backgroundColor = .white
      view.addSubview(contentV)
      dateTPicker.frame = CGRect(x:0,y:0,width: contentV.ks_width,height: 260)
      
      contentV.addSubview(dateTPicker)
    contentV.addSubview(confirmBtn)
      confirmBtn.snp.makeConstraints { make in
          make.centerX.equalTo(contentV)
          make.bottom.equalTo(contentV).offset(-6)
          make.width.equalTo(100)
          make.height.equalTo(35)
      }
      
   confirmBtn.addTarget(self, action: #selector(confirm), for: .touchUpInside)
 
  }
    //日期选择器响应方法
    @objc func dateChanged(datePicker : UIDatePicker){
        //更新提醒时间文本框
        let formatter = DateFormatter()
        //日期样式
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
         self.selectTime = formatter.string(from: datePicker.date)
    }

 
    @objc func confirm(){
        if let clo = self.resultClosure {
            clo(self.selectTime)
        }
        self.close()
    }
    func present() {
    
        let currentVC = RouterUtils.currentTopViewController()!
        self.modalPresentationStyle = .overFullScreen
        currentVC.present(self, animated: false, completion: nil)
    }
  @objc func close(){
     
      self.dismiss(animated: false)
      
      
      
  }
  
  
}
